define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				"class": 'P-financeDetail'
				,urlRemove: 'adexchange/remove'
				,urlRecover: 'adexchange/recover'
				,urlAttr: 'adexchange/attrUpdate'
			});

			this.Super('init', arguments);
			this.$updateRight = true;//pubjs.checkRight('adexchange.update');
		},
		afterBuild:function(){
			this.append($([
				'<div class="M-formHead">',
					'<label class="M-formHeadTitle">'+LANG('BiddingX投放渠道管理')+'</label>',
					'<div class="mb10">',
						// '<span class="" pub-name="batch" pub-mod="@base/common/input.button" pub-auth="adexchange.add"/>',
						// '<span class="ml10" pub-name="batchProp" pub-mod="@base/common/input.button" pub-auth="adexchange.add"/>',
						'<span class="addButton mb10" pub-name="addButton" pub-mod="@base/common/input.button" pub-auth="adexchange.add"></span>',
						'<span class="ml10" pub-name="weight" pub-mod="@base/common/input.button" pub-auth="adexchange.weight"/>',
					'</div>',
				'</div>',
				'<div class="cl" pub-name="highGrid" pub-mod="adExchange/grid/list.base"/>'
			].join('')));

			var self = this;
			var c = self.getConfig();
			var config = {
				'addButton':{
					'value':LANG('新增渠道')
					,'class': 'btn btn-success'
					,'width': 115
				}
				,'weight':{
					'value':LANG('渠道权值设置')
					,'class': 'btn btn-success'
					,'width': 115
				}
				// ,'batch':{
				// 	'value':LANG('批量导入渠道')
				// 	,'class': 'btn btn-success'
				// 	,'width': 115
				// }
				// ,'batchProp':{
				// 	'value':LANG('批量导入属性')
				// 	,'class': 'btn btn-success'
				// 	,'width': 115
				// }
				,'highGrid': {
					'hasSearch': true,
					'hasFilter': true
				}
			};

			self.buildFromTemplate(config);
			self.createAsync('editAdExchangePopwin','adExchange/popwin/edit.base', function( mod ) {
				self.$.editPopwin = mod;
			});

			self.createAsync('batchPopwin','adExchange/popwin/edit.batch');
			self.createAsync('batchPropPopwin','adExchange/popwin/edit.batchProp');
			self.createAsync('aliasPopwin','adExchange/popwin/edit.alias');
			self.createAsync('creativeLimitPopwin','adExchange/popwin/edit.creativeLimit');

			self.createAsync('sidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'items': [
					{'name': 'IsDeleted', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('状态：'),
						'data': [
							{id: 0, name: LANG('正常')},
							{id: 1, name: LANG('删除')}
						],
						'value': 0
					}},
					{'name': 'attr', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('属性：'),
						'all': true,
						'url': 'adexchange/getAttributes',
						'param':{boolValOnly:true},
						'reqType': 'websocket',
						'key': 'Name',
						'def': '请选择属性',
						'name': 'Name',
						'value': 0
					}}
				]
			});
		},
		onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;

				switch (type) {
					case 'IsDeleted':
						param.IsDeleted = !!id;
						break;
					case 'attr':
						param.Attr = id;
						break;
				}
			});

			self.$.highGrid.setParam(param).load();
		},
		onSaveRecord: function() {
			var self = this;
			self.$.highGrid.reload();
		},
		onSaveAlias: function(ev) {
			if(ev.name == 'aliasPopwin')
			{
				var self = this;
				var c = this.getConfig();
				pubjs.loading.show();
				ev.param.type = 'Alias';
				pubjs.mc.send(c.urlAttr, ev.param, function(err, data) {
					pubjs.loading.hide();
					if(err)
					{
						return pubjs.alert(err.message);
					}

					pubjs.alert(LANG('更新成功'));
					self.$.highGrid.reload();
				})
			}
		},
		onSaveCreativeLimit: function(ev) {
			if(ev.name == 'creativeLimitPopwin')
			{
				var self = this;
				var c = this.getConfig();
				pubjs.loading.show();
				ev.param.type = 'DynamicCreateLimit';
				pubjs.mc.send(c.urlAttr, ev.param, function(err, data) {
					pubjs.loading.hide();
					if(err)
					{
						return pubjs.alert(err.message);
					}

					pubjs.alert(LANG('更新成功'));
					self.$.highGrid.reload();
				})
			}
		},
		/**
		 * 加载数据
		 * @param  {Number} id 账户id
		 * @return {Object}    模块实例
		 */
		load:function(){
			return this;
		},
		reload: function(param){
			this.load();
			this.$.highGrid.reload(param);
			return this;
		},
		onEditSuccess:function(){
			this.$.highGrid.reload();
		},
		setData:function(cost){
			return this;
		},
		editPopwin:function(adExchange){
			var self = this;
			if (self.$.editPopwin) {
				self.$.editPopwin.setData(adExchange).show();
			}
		},
		onInputClick:function(ev){
			var mod;
			switch (ev.name) {
				case 'weight':
					return pubjs.controller.navigate('adExchange/weight');
					break;
				case 'addButton':
					mod = this.$.editPopwin;
					break;
				case 'batch':
					mod = this.$.batchPopwin;
					break;
				case 'batchProp':
					mod = this.$.batchPropPopwin;
					break;
				default :
					break;
			}

			mod && mod.reset().show();
		}
		,onOperateMenuShow: function(ev){
			var param = ev.param;
			var result = [];

			//检查是否有编辑权限
			result.push({id: 'edit', name: LANG('编辑')});
			if(!param.IsDeleted)
			{
				result.push({id: 'remove', name: LANG('删除')});
			}
			if(param.IsDeleted)
			{
				result.push({id: 'recover', name: LANG('恢复')});
			}

			result.push({id: 'alias', name: LANG('别名')});
			result.push({id: 'create_limit', name: LANG('动态创意限制用户')});
			ev.returnValue = result;
			return false;
		}
		// 操作菜单-响应菜单操作
		,onOperateMenuSelect: function(ev){
			var op = ev.param[0];
			var data = ev.param[1];
			var c = this.getConfig();
			var id = +data.id;
			var self = this;
			var mod;

			switch(op.key){
				case 'edit':
					self.editPopwin(data);
					break;
				case 'remove':
					pubjs.confirm(LANG('确认要删除此记录吗'), function(res) {
						if(res) {
							pubjs.loading.show();
							pubjs.mc.send(c.urlRemove, {id: id}, function(err) {
								pubjs.loading.hide();
								if(err) {
									pubjs.alert(err.message);
									return false;
								}

								pubjs.alert(LANG('删除成功'));
								self.reload();
							});
						}
					});
					break;
				case 'recover':
					var url =  c.urlRecover;
					pubjs.loading.show();
					pubjs.mc.send(url, {id: id}, function(err) {
						pubjs.loading.hide();
						if(err) {
							pubjs.alert(err.message);
							return false;
						}

						pubjs.alert(LANG('恢复成功'));
						self.reload();
					});
					break;
				case 'alias':
					mod = this.$.aliasPopwin;
					mod && mod.reset(id).setData(data).show();
					break;
				case 'create_limit':
					mod = this.$.creativeLimitPopwin;
					mod && mod.reset(id).setData(data).show();
					break;
				default :
					return;
			}

			return false;
		}
	});

	exports.main = List;

});